package com.yugao.fintech.vctgone.admin.controller;

import com.yugao.fintech.vctgone.admin.pojo.dto.RoleDTO;
import com.yugao.fintech.vctgone.admin.pojo.dto.RoleMenuAllocationDTO;
import com.yugao.fintech.vctgone.admin.pojo.dto.RoleUserAllocationDTO;
import com.yugao.fintech.vctgone.admin.pojo.query.RoleMemberQuery;
import com.yugao.fintech.vctgone.admin.pojo.vo.RoleMemberVO;
import com.yugao.fintech.vctgone.admin.pojo.vo.RoleVO;
import com.yugao.fintech.vctgone.admin.service.RoleMenuService;
import com.yugao.fintech.vctgone.admin.service.RoleService;
import com.yugao.fintech.vctgone.admin.service.UserRoleService;
import com.yugao.fintech.vctgone.admin.service.UserService;
import com.yugao.fintech.vctgone.mybatisplus.base.BaseQuery;
import com.yugao.fintech.vctgone.mybatisplus.util.PageUtil;
import com.yugao.fintech.vctgone.web.util.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Tag(name = "角色管理")
@RequiredArgsConstructor
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RoleController {
    private final RoleService roleService;
    private final UserService userService;
    private final RoleMenuService roleMenuService;
    private final UserRoleService userRoleService;

    @GetMapping
    @Operation(summary = "获取角色")
    @PreAuthorize("hasAuthority('sys_role_query')")
    public ResultVO<PageUtil<RoleVO>> getRoles(BaseQuery query) {
        return ResultVO.success(roleService.getRoles(query));
    }

    @PostMapping
    @Operation(summary = "新增角色")
    @PreAuthorize("hasAuthority('sys_role_add')")
    public ResultVO<?> saveRole(@RequestBody @Valid RoleDTO dto) {
        roleService.saveOrUpdate(dto);
        return ResultVO.success();
    }

    @PutMapping
    @Operation(summary = "编辑角色")
    @PreAuthorize("hasAuthority('sys_role_edit')")
    public ResultVO<?> updateRole(@RequestBody @Valid RoleDTO dto) {
        roleService.saveOrUpdate(dto);
        return ResultVO.success();
    }

    @DeleteMapping("/{ids}")
    @Operation(summary = "删除角色")
    @PreAuthorize("hasAuthority('sys_role_delete')")
    public ResultVO<?> deleteRole(@PathVariable String ids) {
        roleService.deleteRole(ids);
        return ResultVO.success();
    }

    @GetMapping("/allRoles")
    @Operation(summary = "获取所有角色")
    public ResultVO<List<RoleVO>> allRoles() {
        return ResultVO.success(roleService.getAllRoles());
    }

    @GetMapping("/roleMembers")
    @Operation(summary = "获取角色成员")
    public ResultVO<PageUtil<RoleMemberVO>> getRoleMembers(@Valid RoleMemberQuery queryParam) {
        return ResultVO.success(userService.getRoleMembers(queryParam));
    }

    @PostMapping("/roleUserAllocation")
    @Operation(summary = "角色用户分配")
    @PreAuthorize("hasAuthority('sys_role_user_allocation')")
    public ResultVO<?> allocateRoleUser(@RequestBody RoleUserAllocationDTO dto) {
        userRoleService.allocationRoleUser(dto);
        return ResultVO.success();
    }

    @PostMapping("/roleMenuAllocation")
    @Operation(summary = "角色菜单分配")
    @PreAuthorize("hasAuthority('sys_role_menu_allocation')")
    public ResultVO<?> allocateRoleMenu(@RequestBody @Valid RoleMenuAllocationDTO dto) {
        roleMenuService.roleMenuAllocation(dto);
        return ResultVO.success();
    }
}
